﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using BaocaoNPC.common;
using BaocaoNPC.Usercontrols.reports;

namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for Nhap_KHTTCC.xaml
    /// </summary>
    public partial class Nhap_KHTTCC : UserControl
    {
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        public DataSet ds_KHTRAMCC = new DataSet();
        public String p_madvi = NPCUS_DanhMuc.Pglobal.strMa_dviqly.ToString();  //CMISLibrary.Libraries.UserInfomation.MaDViqly.ToString();
        public Nhap_KHTTCC()
        {
            InitializeComponent();
            try
            {
                GlobalServices.Service_NPCUS.CapnhatdstramCC_KH(p_madvi);
                loadgrid();
                btnGhi.IsEnabled = false;
                btnHuy.IsEnabled = false;
                btnnhap.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void btnnhap_Click(object sender, RoutedEventArgs e)
        {
            btnGhi.IsEnabled = true;
            btnHuy.IsEnabled = true;
            btnnhap.IsEnabled = false;
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnGhi.IsEnabled = false;
                btnHuy.IsEnabled = false;
                btnnhap.IsEnabled = true;
                loadgrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (ds_KHTRAMCC != null)
                {
                    if (ds_KHTRAMCC.Tables.Count > 0)
                    {
                        //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                        if (GlobalServices.Service_NPCUS.update_khttcc(ds_KHTRAMCC))
                        //if(bus.update_khttcc(ds_KHTRAMCC))
                        {
                            MessageBox.Show("Cập nhật số liệu thành công");
                            //---load lai grid
                            loadgrid();

                            btnGhi.IsEnabled = false;
                            btnHuy.IsEnabled = false;
                            btnnhap.IsEnabled = true;
                        }
                        else { MessageBox.Show("Lỗi trong quá trình cập nhật"); }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

        }

        private void loadgrid()
        {
            try
            {

                ds_KHTRAMCC.Clear();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                //ds_KHTRAMCC = bus.Load_KHOACHTRAMCC(p_madvi);
                ds_KHTRAMCC = GlobalServices.Service_NPCUS.Load_KHOACHTRAMCC(p_madvi);
                if (ds_KHTRAMCC != null)
                {
                    if (ds_KHTRAMCC.Tables.Count > 0)
                    {
                        ds_KHTRAMCC.Tables[0].TableName = "KHTTCC";
                        dgr_KHTRAMCC.ItemsSource = ds_KHTRAMCC.Tables["KHTTCC"].DefaultView;
                        ds_KHTRAMCC.Tables["KHTTCC"].DefaultView.AllowNew = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString()); return;
            }
        }
    }
}
